﻿using Microsoft.AspNetCore.SignalR;
using System.IdentityModel.Tokens.Jwt;

namespace FrameworkCore.Hubs;

public static class HubCallerContextExtensions
{
    public static string GetAccessToken(this HubCallerContext context)
    {
        return context?.GetHttpContext()?.Request.Query["access_token"];
    }

    public static string GetUserId(this HubCallerContext context)
    {
        var jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
        var jwtSecurityToken = jwtSecurityTokenHandler.ReadJwtToken(context.GetAccessToken());
        var claims = jwtSecurityToken?.Claims?.ToArray();
        return claims?[0].Value;
    }
}
